package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import com.mars.module.admin.entity.SysUserCollection;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysUserLikeCommentRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysUserLikeCommentMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysUserLikeComment;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysUserLikeCommentService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 点赞评论业务层处理
 *
 * @author mars
 * @date 2024-03-24
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysUserLikeCommentServiceImpl implements ISysUserLikeCommentService {

    private final SysUserLikeCommentMapper baseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysUserLikeComment add(SysUserLikeCommentRequest request) {
        SysUserLikeComment entity = SysUserLikeComment.builder().build();
        BeanUtils.copyProperties(request, entity);
        Long userId = ContextUserInfoThreadHolder.get().getId();
        entity.setUserId(userId);
        Long l = baseMapper.selectCount(new QueryWrapper<SysUserLikeComment>().eq("comment_id", request.getCommentId()).eq("user_id", userId));
        if (l == 0) {
            baseMapper.insert(entity);
        }
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysUserLikeCommentRequest request) {
        SysUserLikeComment entity = SysUserLikeComment.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysUserLikeComment getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<SysUserLikeComment> pageList(SysUserLikeCommentRequest request) {
        Page<SysUserLikeComment> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysUserLikeComment> query = this.buildWrapper(request);
        IPage<SysUserLikeComment> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysUserLikeComment> list(SysUserLikeCommentRequest request) {
        LambdaQueryWrapper<SysUserLikeComment> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    @Override
    public void deleteByCommentId(String id) {
        Long userId = ContextUserInfoThreadHolder.get().getId();
        baseMapper.delete(new QueryWrapper<SysUserLikeComment>().eq("comment_id", id).eq("user_id", userId));
    }

    private LambdaQueryWrapper<SysUserLikeComment> buildWrapper(SysUserLikeCommentRequest param) {
        LambdaQueryWrapper<SysUserLikeComment> query = new LambdaQueryWrapper<>();
        return query;
    }

}
